"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _vue = _interopRequireDefault(require("vue"));

var _Toast = _interopRequireDefault(require("./Toast"));

var _utils = require("../utils");

var defaultOptions = {
    icon: '',
    type: 'text',
    // @deprecated
    mask: false,
    value: true,
    message: '',
    className: '',
    overlay: false,
    onClose: null,
    onOpened: null,
    duration: 2000,
    iconPrefix: undefined,
    position: 'middle',
    transition: 'van-fade',
    forbidClick: false,
    loadingType: undefined,
    getContainer: 'body',
    overlayStyle: null,
    closeOnClick: false,
    closeOnClickOverlay: false
}; // default options of specific type

var defaultOptionsMap = {};
var queue = [];
var multiple = false;
var currentOptions = (0, _extends2.default)({}, defaultOptions);

function parseOptions(message) {
    if ((0, _utils.isObj)(message)) {
        return message;
    }

    return {
        message: message
    };
}

function createInstance() {
    /* istanbul ignore if */
    if (_utils.isServer) {
        return {};
    }

    if (!queue.length || multiple) {
        var toast = new (_vue.default.extend(_Toast.default))({
            el: document.createElement('div')
        });
        toast.$on('input', function (value) {
            toast.value = value;
        });
        queue.push(toast);
    }

    return queue[queue.length - 1];
} // transform toast options to popup props


function transformOptions(options) {
    return (0, _extends2.default)({}, options, {
        overlay: options.mask || options.overlay,
        mask: undefined,
        duration: undefined
    });
}

function Toast(options) {
    if (options === void 0) {
        options = {};
    }

    var toast = createInstance(); // should add z-index if previous toast has not disappeared

    if (toast.value) {
        toast.updateZIndex();
    }

    options = parseOptions(options);
    options = (0, _extends2.default)({}, currentOptions, {}, defaultOptionsMap[options.type || currentOptions.type], {}, options);

    options.clear = function () {
        toast.value = false;

        if (options.onClose) {
            options.onClose();
        }

        if (multiple && !_utils.isServer) {
            toast.$on('closed', function () {
                clearTimeout(toast.timer);
                queue = queue.filter(function (item) {
                    return item !== toast;
                });
                var parent = toast.$el.parentNode;

                if (parent) {
                    parent.removeChild(toast.$el);
                }

                toast.$destroy();
            });
        }
    };

    (0, _extends2.default)(toast, transformOptions(options));
    clearTimeout(toast.timer);

    if (options.duration > 0) {
        toast.timer = setTimeout(function () {
            toast.clear();
        }, options.duration);
    }

    return toast;
}

var createMethod = function createMethod(type) {
    return function (options) {
        return Toast((0, _extends2.default)({
            type: type
        }, parseOptions(options)));
    };
};

['loading', 'success', 'fail'].forEach(function (method) {
    Toast[method] = createMethod(method);
});

Toast.clear = function (all) {
    if (queue.length) {
        if (all) {
            queue.forEach(function (toast) {
                toast.clear();
            });
            queue = [];
        } else if (!multiple) {
            queue[0].clear();
        } else {
            queue.shift().clear();
        }
    }
};

Toast.setDefaultOptions = function (type, options) {
    if (typeof type === 'string') {
        defaultOptionsMap[type] = options;
    } else {
        (0, _extends2.default)(currentOptions, type);
    }
};

Toast.resetDefaultOptions = function (type) {
    if (typeof type === 'string') {
        defaultOptionsMap[type] = null;
    } else {
        currentOptions = (0, _extends2.default)({}, defaultOptions);
        defaultOptionsMap = {};
    }
};

Toast.allowMultiple = function (value) {
    if (value === void 0) {
        value = true;
    }

    multiple = value;
};

Toast.install = function () {
    _vue.default.use(_Toast.default);
};

_vue.default.prototype.$toast = Toast;
var _default = Toast;
exports.default = _default;